import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.text.DecimalFormat;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.GroundItems;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Keyboard;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Players;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSGroundItem;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Foulwerp"}, name="Chickens", keywords={"Combat"}, description="Kills Chickens & Loots Feathers", version=1.03D)
public class Chickens extends Script
  implements PaintListener
{
  private static final DecimalFormat k = new DecimalFormat("#.#");
  private static final int[] skill = { 0, 1, 2, 3, 4, 6 };
  private static final int[] drop = { 526, 1925, 1944, 2138 };
  private static final String[] skillNames = { "Attack", "Defence", "Strength", "Constitution", "Range", "Magic" };

  private static final Color[] skillColors = { new Color(145, 25, 25).brighter(), new Color(95, 115, 185), Color.GREEN.darker(), Color.WHITE.darker(), new Color(70, 95, 20).brighter(), new Color(95, 115, 230) };
  private long start;
  private int[] startXP;
  private RSArea area;
  private static final RSArea[] areas = { new RSArea(3014, 3282, 3020, 3298), new RSArea(3022, 3281, 3037, 3290), new RSArea(3225, 3295, 3236, 3301), new RSArea(3195, 2252, 3198, 3359) };

  public boolean onStart()
  {
    for (int i = 0; i < areas.length; i++) {
      if (areas[i].contains(new RSTile[] { this.players.getMyPlayer().getLocation() })) {
        this.area = areas[i];
        switch (i) {
        case 0:
          log("Falador Chickens Inside Pen...");
          break;
        case 1:
          log("Falador Chickens Outside House...");
          break;
        case 2:
          log("Lumbridge Chickens...");
          break;
        case 3:
          log("Champions Guild Chickens...");
        }
      }
    }

    if (this.area == null) {
      log("Not In A Suppoted Chicken Location...");
      return false;
    }
    this.startXP = new int[6];
    for (i = 0; i < skill.length; i++) {
      this.startXP[i] = this.skills.getCurrentExp(skill[i]);
    }
    this.start = System.currentTimeMillis();
    return true;
  }

  public int loop()
  {
    this.mouse.setSpeed(random(4, 7));
    if (!this.game.isLoggedIn()) {
      return 3000;
    }
    if (this.players.getMyPlayer().getInteracting() != null) {
      if (this.interfaces.canContinue()) {
        this.interfaces.clickContinue();
      }
      return random(900, 1100);
    }
    RSItem localRSItem = this.inventory.getItem(drop);
    if (localRSItem != null) {
      localRSItem.doAction("Drop");
      return random(700, 900);
    }
    RSGroundItem localRSGroundItem = pickup();
    if (localRSGroundItem != null) {
      if (this.players.getMyPlayer().isMoving()) {
        return random(400, 600);
      }
      if (!localRSGroundItem.isOnScreen()) {
        this.camera.turnTo(localRSGroundItem.getLocation(), 15);
        if (!localRSGroundItem.isOnScreen()) {
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(localRSGroundItem.getLocation()));

          return random(900, 1200);
        }
      }
      localRSGroundItem.doAction("Take Feather");
      return random(900, 1100);
    }
    if ((!this.walking.isRunEnabled()) && (this.walking.getEnergy() > 60)) {
      this.walking.setRun(true);
      return random(750, 1000);
    }
    RSNPC localRSNPC = newNPC();
    if (localRSNPC == null) {
      if (this.calc.distanceTo(this.area.getCentralTile()) > 3) {
        this.walking.walkTileMM(this.area.getCentralTile());
      }
      return random(800, 1000);
    }
    if ((this.players.getMyPlayer().isMoving()) && (!localRSNPC.isOnScreen())) {
      return random(400, 600);
    }
    if (!localRSNPC.isOnScreen()) {
      turnTo(this.camera.getCharacterAngle(localRSNPC), localRSNPC);
      if (!localRSNPC.isOnScreen()) {
        this.walking.walkTileMM(this.walking.getClosestTileOnMap(localRSNPC.getLocation()));

        return random(800, 1000);
      }
    }
    localRSNPC.doAction(new StringBuilder().append("Attack ").append(localRSNPC.getName()).toString());
    return random(900, 1100);
  }

  public void onRepaint(Graphics paramGraphics)
  {
    int i = 365; int j = 356; int m = 196; int n = 201;
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    long l1 = System.currentTimeMillis() - this.start;
    localGraphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    localGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Point localPoint = this.mouse.getLocation();
    localGraphics2D.drawLine((int)localPoint.getX() - 3, (int)localPoint.getY(), (int)localPoint.getX() + 3, (int)localPoint.getY());

    localGraphics2D.drawLine((int)localPoint.getX(), (int)localPoint.getY() - 3, (int)localPoint.getX(), (int)localPoint.getY() + 3);

    localGraphics2D.setFont(new Font("Comic Sans MS", 0, 10));
    localGraphics2D.setColor(Color.BLACK);
    localGraphics2D.drawRect(m, 345, 300, 11);
    localGraphics2D.setColor(new Color(0, 0, 0, 220));
    localGraphics2D.fillRect(m, 345, 300, 11);
    localGraphics2D.setColor(Color.WHITE);
    localGraphics2D.drawString(new StringBuilder().append("Chickens - Run Time: ").append(format(l1)).append(" - Version 1.03").toString(), n, 354);

    for (int i2 = 0; i2 < 6; i2++) {
      int i1 = this.skills.getCurrentExp(skill[i2]) - this.startXP[i2];
      if (i1 > 0) {
        double d = i1 * 3600000.0D / (System.currentTimeMillis() - this.start);
        long l2 = ()(this.skills.getExpToNextLevel(skill[i2]) * 3600000.0D / d);
        localGraphics2D.setColor(Color.BLACK);
        localGraphics2D.drawRect(m, j, 300, 11);
        localGraphics2D.setColor(new Color(0, 0, 0, 220));
        localGraphics2D.fillRect(m, j, 300, 11);
        localGraphics2D.setColor(skillColors[i2]);
        localGraphics2D.drawString(new StringBuilder().append(skillNames[i2]).append(": ").append(k.format(i1 / 1000.0D)).append(" K Earned - ").append(k.format(d / 1000.0D)).append(" K P/H - ").append(format(l2)).append(" TTL").toString(), n, i);

        i += 11;
        j += 11;
      }
    }
  }

  private String format(long paramLong) {
    if (paramLong <= 0L) {
      return "--:--:--";
    }
    StringBuilder localStringBuilder = new StringBuilder();
    long l1 = paramLong / 1000L;
    long l2 = l1 / 60L;
    long l3 = l2 / 60L;
    int i = (int)l1 % 60;
    int j = (int)l2 % 60;
    int m = (int)l3;
    if (m < 10) {
      localStringBuilder.append("0");
    }
    localStringBuilder.append(m);
    localStringBuilder.append(":");
    if (j < 10) {
      localStringBuilder.append("0");
    }
    localStringBuilder.append(j);
    localStringBuilder.append(":");
    if (i < 10) {
      localStringBuilder.append("0");
    }
    localStringBuilder.append(i);
    return localStringBuilder.toString();
  }

  private RSGroundItem pickup() {
    return this.groundItems.getNearest(new Chickens.1(this));
  }

  private RSNPC newNPC()
  {
    RSNPC localRSNPC = interactingNPC();
    return localRSNPC != null ? localRSNPC : this.npcs.getNearest(new Chickens.2(this));
  }

  private RSNPC interactingNPC()
  {
    return this.npcs.getNearest(new Chickens.3(this));
  }

  public void turnTo(int paramInt, RSNPC paramRSNPC)
  {
    char c1 = '%';
    char c2 = '\'';
    char c3 = c1;
    int i = this.camera.getAngle();
    if (i < 180) {
      i += 360;
    }
    if (paramInt < 180) {
      paramInt += 360;
    }
    if (paramInt > i) {
      if (paramInt - 180 < i)
        c3 = c2;
    }
    else if ((i > paramInt) && 
      (i - 180 >= paramInt)) {
      c3 = c2;
    }

    paramInt %= 360;
    this.keyboard.pressKey(c3);
    int j = 0;

    while (((!paramRSNPC.isOnScreen()) && (this.camera.getAngle() > paramInt + 10)) || ((!paramRSNPC.isOnScreen()) && (this.camera.getAngle() < paramInt - 10))) {
      sleep(10);
      j += 10;
      if (j > 500) {
        int m = j - 500;
        if (m == 0)
          this.keyboard.pressKey(c3);
        else if (m % 40 == 0) {
          this.keyboard.pressKey(c3);
        }
      }
    }
    this.keyboard.releaseKey(c3);
  }
}